package com.animee.day11.slice;
import com.animee.day11.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.Text;

public class BMIInfoAbilitySlice extends AbilitySlice implements Component.ClickedListener {
    private double sg,tz,bmi;
    Image bmiImg;
    Text staTv,regTv,bmiTv;
    Button resetBtn;
    private String sex;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_abilitiy_bmi_info);
        //获取上一个页面的数据
        sg = intent.getDoubleParam("sg",0);
        tz = intent.getDoubleParam("tz", 0);
        bmi = intent.getDoubleParam("bmi", 0);
        sex = intent.getStringParam("sex");
        initView();
        setView();
    }

    private void setView() {
        //保留2位小数
        String bmiStr= String.format("%.2f", bmi);
        bmiTv.setText(bmiStr);
        if (sex.equals("男")) {
            if (bmi<18){
                staTv.setText("偏瘦状态");
                bmiImg.setPixelMap(ResourceTable.Media_bmi_1);
            }else if (bmi<22){
                staTv.setText("健康状态");
                bmiImg.setPixelMap(ResourceTable.Media_bmi_2);
            }else if (bmi<25){
                staTv.setText("肥胖状态");
                bmiImg.setPixelMap(ResourceTable.Media_bmi_3);
            }else {
                staTv.setText("重度肥胖");
                bmiImg.setPixelMap(ResourceTable.Media_bmi_4);
            }
            double start = sg*sg*18;
            double end = sg*sg*22;
            String starStr = String.format("%.2f", start);
            String endStr = String.format("%.2f", end);
            String msg = starStr+"kg~"+endStr+"kg";
            regTv.setText(msg);
        }else{
            if (bmi<16){
                staTv.setText("偏瘦状态");
                bmiImg.setPixelMap(ResourceTable.Media_bmi_1);
            }else if (bmi<20){
                staTv.setText("健康状态");
                bmiImg.setPixelMap(ResourceTable.Media_bmi_2);
            }else if (bmi<23){
                staTv.setText("肥胖状态");
                bmiImg.setPixelMap(ResourceTable.Media_bmi_3);
            }else {
                staTv.setText("重度肥胖");
                bmiImg.setPixelMap(ResourceTable.Media_bmi_4);
            }
            double start = sg*sg*16;
            double end = sg*sg*20;
            String starStr = String.format("%.2f", start);
            String endStr = String.format("%.2f", end);
            String msg = starStr+"kg~"+endStr+"kg";
            regTv.setText(msg);
        }
    }

    private void initView() {
        bmiImg = findComponentById(ResourceTable.Id_bmiinfo_iv);
        staTv = findComponentById(ResourceTable.Id_bmiinfo_tv_zt);
        regTv = findComponentById(ResourceTable.Id_bmiinfo_tv_tz);
        bmiTv = findComponentById(ResourceTable.Id_bmiinfo_tv_bmi);
        resetBtn = findComponentById(ResourceTable.Id_bmiinfo_btn);
        resetBtn.setClickedListener(this);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onClick(Component component) {
        if (component.getId() == ResourceTable.Id_bmiinfo_btn) {
            //关闭当前页面
            terminate();
        }
    }
}
